<?php
class ClsNaanalInstall
{
    private $xmlfile=array();
    private $objPDO=null;
    private $module="";
    private $database="";
    public function __construct($module,$database)
    {
        $this->module=$module;
        $this->database=$database;
        $this->objPDO=ClsNaanalPDO::getModuleInstance($module);
    }
    ///add xml install file with path
    public function addXML($xmlfile)
    {
        $this->xmlfile=$xmlfile;
    }
    public function render()
    {
        $dom=simplexml_load_file($this->xmlfile);
        $tableName=(string)$dom->table["name"];
        //if(!isTableExist($this->database, $tableName))
        //{
        $sql="CREATE TABLE IF NOT EXISTS {$tableName} (id INT PRIMARY KEY NOT NULL AUTO_INCREMENT)";
        $this->objPDO->setQuery($sql);
        $sql="SHOW COLUMNS FROM {$tableName}";
        $this->objPDO->setQuery($sql);
        $arrColumn=$this->objPDO->getColumn("Field");
        $arrFieldSql=array();
        foreach($dom->table->field as $elem)
        {
            $type="text";
            $name=(string)$elem["name"];
            if(!in_array($name, $arrColumn))
            {
                $size=0;
                if($elem["type"]=="string")
                {
                    $size=255;
                    if(isset($elem["size"]))
                    {
                        $size=(string)$elem["size"];
                    }
                    $type="VARCHAR";
                }
                else if($elem["type"]=="integer")
                {
                    $size=14;
                    if(isset($elem["size"]))
                    {
                        $size=(string)$elem["size"];
                        $size=(int)$size;
                    }
                    $type="INT";
                }
                else
                {
                    $type="TEXT";
                }
                if($size>0)
                {
                    $arrFieldSql[]="ADD COLUMN `{$name}` {$type}($size)";
                }
                else
                {
                    $arrFieldSql[]="ADD COLUMN `{$name}` {$type}";
                }
            }
        }
        if($arrFieldSql)
        {
            $sql="ALTER TABLE `{$tableName}` ".implode(",",$arrFieldSql);
            $this->objPDO->setQuery($sql);
        }
        //}
    }
}
?>